package com.kgcx.common.utils;

import com.kgcx.common.constant.Cons;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Calendar;
import java.util.Date;

public class TokenUtils {
    public static final String[] TOKEN_INNER_SERVER_ID = {"organization", "order", "system", "price", "coupon", "schedule"};
    public static final String TOKEN_SUBJECT_SERVER = "server";
    public static final String TOKEN_SUBJECT_DRIVER = "driver";
    public static final String TOKEN_SUBJECT_CUSTOMER = "customer";
    public static final String TOKEN_SUBJECT_WEBUSER = "webuser";

    public static final String[] TOKEN_SKIP_PATH = {"/organization/driver/login"
            , "/organization/driver/code"
            , "/organization/driver/resetPassword"
            , "/organization/customer/login"
            , "/organization/customer/code"
            //支付回调地址
            , "/order/pay/weChat-callback", "/order/pay/ali-pay-callback"
            //小程序获取openId地址				
            , "/system/wx/getLiteAppOpenid"
            //oa登录
            , "/system/sys/user/sysUserlogin"
            //小程序免登录接口，查询城市、查询航站楼、查询高铁站出口
            , "/system/gis/getCity", "/organization/city/getCity", "/organization/city/getCityList", "/organization/airport/getAirportTerminalList", "/organization/station/getStationExitList"
            //app乘客端预估价接口
            , "/price/caculationprice/caculatePrice", "/system/sys/v1/menu/queryMenuByRole", "/system/dict/getDictList"
            , "/order/order/getHotAddress"
            , "/order/order/getHotCity"
            , "/organization/suggestAddress/getSuggestAddressList"
            , "/system/appversion/getLastAppVersion"
            , "/system/sys/user/code"
            , "/schedule/v1/schedule/settings/getOptionallyBook"
            , "/schedule/v1/schedule/settings/getSpeciallyBook"
            , "/schedule/v1/schedule/settings/getBookParame"
            , "/system/tools/getSysTime"
    };

    public static String getToken(String id, String subject, String issuer) {
        Calendar c = Calendar.getInstance();
        Date time = c.getTime();
        c.add(Calendar.MONTH, 6);
        JwtBuilder builder = Jwts.builder()
                .setId(id)
                .setIssuedAt(time)
                .setExpiration(c.getTime())
                .setSubject(subject).setIssuer(issuer)
                .signWith(SignatureAlgorithm.HS256, Cons.MD5_KEY);
        return builder.compact();
    }

    public static String getToken(Long id, String subject, String issuer) {
        return getToken(id.toString(), subject, issuer);
    }

    public static String getToken(Long id, String subject) {
        return getToken(id.toString(), subject, null);
    }

    public static String getToken(String id, String subject) {
        return getToken(id, subject, null);
    }

    public static Long getId(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(Cons.MD5_KEY)
                .parseClaimsJws(token).getBody();
        return Long.valueOf(claims.getId());
    }

    public static Claims getClaims(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(Cons.MD5_KEY)
                .parseClaimsJws(token).getBody();
        return claims;
    }

    public static String getTokenKey(Long kid, String subject) {
        return subject + "." + kid;
    }

    public static boolean isInnerServer(String id) {
        return ArrayUtils.contains(TOKEN_INNER_SERVER_ID, id);
    }

    public static boolean isAdminSubject(String subject) {
        return TOKEN_SUBJECT_WEBUSER.equals(subject);
    }

    public static boolean notAdminSubject(String subject) {
        return !isAdminSubject(subject);
    }

    public static boolean notInnerServer(String id) {
        return !isInnerServer(id);
    }
}
